import {
  imageUrl
} from "../utils/imageutil"
import {is_auth} from "../utils/util"
Page({

  data: {
    imageUrl,
    qy_list: ['1、一年免费洗车：从购卡日起，往后一年免费洗车26次；但仅能同一个用户同一辆车；', '2、一年请家政员免中介费：同一个账号支持请家政员免除中介费不限次数；', '3、开通省钱年卡：此项服务购买后不适用无理由退款；'],
    is_qy: false,
    member: false,
    vehicle_member_list: [],
    vehicle_member_curr: 0,
    car_id: '',
    car_info: null,
    address_id: '',
    address_info: null,
    multiIndex: [0, 0, 0, 0, 0],
    work_time: '',
    remark: '',
    gg_info: null,
    order_id: '',
    total: 0,
    is_water: false,
    rand: 0,
    car_coupon: null, //卡券
    is_qrcode: false, //是否有卡券
  },
  onLoad(options) {
    this.setData({
      car_id: options.car_id,
      gg_info: JSON.parse(options.info)
    })
    wx.removeStorageSync('address_id')
    wx.removeStorageSync('car_coupon')
    this.vehicle_member()
    if(this.data.gg_info.pid==3){
      this.cleanlist()
    }else{
      this.receiveVehiclelist()
    }
    if (this.data.gg_info.pid != 1) {
      this.change_total()
    }
    let date = new Date()
    const month = date.getMonth()
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    this.setData({
      multiIndex: [0, month, day - 1, hour, minute]
    })
  },

  onShow() {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (currPage.data.id) {
      this.setData({
        car_id: currPage.data.id
      })
      this.user_vehicle_detail()
    }
    if (wx.getStorageSync('address_id')) {
      this.setData({
        address_id: wx.getStorageSync('address_id')
      })
      this.MyAddressInfo()
    }
    if (wx.getStorageSync('car_coupon')) {
      this.setData({
        car_coupon: wx.getStorageSync('car_coupon')
      })
      this.change_total()
      wx.removeStorageSync('car_coupon')
    }
  },

  vehicle_member() {
    getApp().globalData.posts('/index.php/api/app/user/sorting/ouyoung_api/vehicle_member').then(res => {
      this.setData({
        vehicle_member_list: res.data.data
      })
    })
  },

  cleanlist(){
    getApp().globalData.posts('/index.php/api/ouyoung/contract/sorting/ouyapi/cleanlist',{
      status: 1,
      pid: this.data.gg_info.pid,
      id: this.data.gg_info.id,
    }).then(res=>{
      if (res.data.code == 1 && res.data.data.length > 0) {
        this.setData({
          is_qrcode: true
        })
      }
    })
  },

  receiveVehiclelist() {
    getApp().globalData.posts('/api/app/user/sorting/ouyoung_api/receiveVehiclelist', {
      status: 1,
      pid: this.data.gg_info.pid,
      id: this.data.gg_info.id,
    }).then(res => {
      if (res.data.code == 1 && res.data.data.length > 0) {
        this.setData({
          is_qrcode: true
        })
      }
    })
  },

  user_vehicle_detail() {
    getApp().globalData.posts('/index.php/api/app/user/sorting/ouyoung_api/user_vehicle_detail?id=' + this.data.car_id).then(res => {
      this.setData({
        id: ''
      })
      if (res.data.code == 1) {
        let data = res.data.data
        this.setData({
          car_info: data,
          member: false,
          car_coupon: null
        })
        if (this.data.car_info.is_member == 2) {
          if (this.data.car_info.member_type == 2 && (this.data.car_info.total_number - this.data.car_info.use_number == 0)) {
            this.setData({
              total: this.data.gg_info.price
            })
          } else {
            this.setData({
              total: 0,
            })
          }
        } else {
          this.setData({
            total: this.data.gg_info.price
          })
        }
      } else {
        this.setData({
          car_info: null,
          car_id: '',
          total: 0
        })
      }
    })
  },

  bottomTap() {
    if (!is_auth(true)) {
      return false
    }
    if (this.data.gg_info.pid == 1) {
      if (!this.data.car_id) {
        getApp().showToast('请选择服务车辆')
        return
      }
    }
    if (!this.data.address_id) {
      getApp().showToast('请选择服务地址')
      return
    }
    if (!this.data.work_time) {
      getApp().showToast('请选择上门时间')
      return
    }
    if (this.data.gg_info.pid == 1) {
      this.vehiclepay()
    } else if (this.data.gg_info.pid == 3) {
      this.bjtcpay()
    } else {
      this.Hytcpay()
    }
  },

  Hytcpay() {
    let car_coupon_obj = {}
    if (this.data.car_coupon) {
      car_coupon_obj = {
        vehicle_qrcode_id: this.data.car_coupon.id
      }
    }
    getApp().globalData.post({
      data: {
        do: 'Hytcpay',
        a: 'wxapp2',
        uid: wx.getStorageSync('userInfo').id,
        addr_id: this.data.address_id,
        good_id: this.data.gg_info.pid,
        good_gg: this.data.gg_info.id,
        work_time: this.data.work_time,
        remark: this.data.remark,
        ...car_coupon_obj
      }
    }).then(res => {
      let data = res.data.data
      if (res.data.code == 1) {
        this.setData({
          order_id: data.vehicle_order_id,
          rand: data.rand,
        })
        if (data.status == 2) {
          this.setData({
            is_water: true
          })
          return
        }
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          timeStamp: data.timeStamp,
          success: () => {
            this.setData({
              is_water: true
            })
          },
          fail: () => {
            getApp().showToast('支付失败！')
          }
        })
      } else {
        getApp().showModal(res.data.msg)
      }
    })
  },

  bjtcpay(){
    let car_coupon_obj = {}
    if (this.data.car_coupon) {
      car_coupon_obj = {
        vehicle_qrcode_id: this.data.car_coupon.id
      }
    }
    getApp().globalData.post({
      data: {
        do: 'bjtcpay',
        a: 'wxapp2',
        uid: wx.getStorageSync('userInfo').id,
        addr_id: this.data.address_id,
        good_id: this.data.gg_info.pid,
        good_gg: this.data.gg_info.id,
        work_time: this.data.work_time,
        remark: this.data.remark,
        ...car_coupon_obj
      }
    }).then(res => {
      let data = res.data.data
      if (res.data.code == 1) {
        this.setData({
          order_id: data.vehicle_order_id,
          rand: data.rand,
        })
        if (data.status == 2) {
          this.setData({
            is_water: true
          })
          return
        }
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          timeStamp: data.timeStamp,
          success: () => {
            this.setData({
              is_water: true
            })
          },
          fail: () => {
            getApp().showToast('支付失败！')
          }
        })
      } else {
        getApp().showModal(res.data.msg)
      }
    })
  },

  to_back() {
    this.setData({
      is_water: false
    })
    wx.reLaunch({
      url: '/pages/index/blank'
    })
  },
  to_list() {
    this.setData({
      is_water: false
    })
    wx.reLaunch({
      url: './list',
    })
  },

  vehiclepay() {
    let member_id_obj = {}
    let car_coupon_obj = {}
    if (this.data.member) {
      member_id_obj = {
        member_id: this.data.vehicle_member_list[this.data.vehicle_member_curr].id
      }
    }
    if (this.data.car_coupon) {
      car_coupon_obj = {
        vehicle_qrcode_id: this.data.car_coupon.id
      }
    }
    getApp().globalData.post({
      data: {
        do: 'vehiclepay',
        a: 'wxapp2',
        uid: wx.getStorageSync('userInfo').id,
        addr_id: this.data.address_id,
        vehicle_id: this.data.car_id,
        good_id: this.data.gg_info.pid,
        good_gg: this.data.gg_info.id,
        work_time: this.data.work_time,
        remark: this.data.remark,
        ...member_id_obj,
        ...car_coupon_obj
      }
    }).then(res => {
      let data = res.data.data
      if (res.data.code == 1) {
        this.setData({
          order_id: data.vehicle_order_id,
          rand: data.rand,
        })
        if (data.status == 2) {
          this.setData({
            is_water: true
          })
          return
        }
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          timeStamp: data.timeStamp,
          success: () => {
            this.setData({
              is_water: true
            })
          },
          fail: () => {
            getApp().showToast('支付失败！')
          }
        })
      } else {
        getApp().showModal(res.data.msg)
      }
    })
  },

  Vehiclepayv2(e) {
    let order_id = this.data.order_id;
    getApp().globalData.post({
      data: {
        do: 'Vehiclepayv2',
        a: 'wxapp2',
        order_id
      }
    }).then(res => {
      let data = res.data.data;
      if (res.data.code == 1) {
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          timeStamp: data.timeStamp,
          success: () => {
            wx.reLaunch({
              url: './list',
            })
          },
          fail: () => {
            getApp().showToast('支付失败！')
          }
        })
      } else {
        getApp().showModal(res.data.msg)
      }
    })
  },

  MyAddressInfo() { //选择地址
    getApp().globalData.post({
      data: {
        do: "MyAddressInfo",
        id: this.data.address_id
      }
    }).then(res => {
      this.setData({
        address_info: res.data
      })
    })
  },

  getTime(e) {
    this.setData({
      work_time: e.detail
    })
  },
  change_remark(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  change_member() {
    this.setData({
      member: !this.data.member
    })
    this.change_total()
  },
  chang_vehicle_member_curr(e) {
    this.setData({
      vehicle_member_curr: e.currentTarget.dataset.index
    })
    this.change_total()
  },
  change_is_qy() {
    this.setData({
      is_qy: !this.data.is_qy
    })
  },
  change_total() {
    let car_coupon_price = 0;
    if (this.data.car_coupon) {
      car_coupon_price = this.data.car_coupon.price
    }
    if (this.data.member) {
      this.setData({
        total: this.data.vehicle_member_list[this.data.vehicle_member_curr].price - car_coupon_price
      })
    } else {
      this.setData({
        total: Number(this.data.gg_info.price - car_coupon_price).toFixed(2)
      })
    }
  },
  to_car() {
    wx.navigateTo({
      url: './car',
    })
  },
  to_address() {
    wx.navigateTo({
      url: '/pages/matron/address?type=1',
    })
  },
  to_coupon() {
    wx.navigateTo({
      url: './coupon?pid=' + this.data.gg_info.pid + '&id=' + this.data.gg_info.id,
    })
  },
})